﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Chat_D_LuX_Client
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        private ChatServiceRef.User _user;

        public Login()
        {
            InitializeComponent();
            txtUsername.Focus();
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            //clear controls and refocus
            txtPassword.Clear();
            txtUsername.Clear();
            txtUsername.Focus();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            //calls service to do its gangsta thang
            //Create service reference

            ChatServiceRef.Chat_ServiceClient sr = new ChatServiceRef.Chat_ServiceClient();

            try
            {
                string username = txtUsername.Text.ToString();
                string password = txtPassword.Password.ToString();

                _user = sr.Login(username, password);
                if (_user.PkId > 0)
                {
                    MainWindow frmMain = new MainWindow(_user);
                    this.Hide();
                    frmMain.Show();
                }
                else
                {
                    txtPassword.Clear();
                    txtUsername.Focus();
                    txtUsername.SelectAll();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void btnPassReset_Click(object sender, RoutedEventArgs e)
        {
            //does password resetting routine
        }

        private void btnRegister_Click(object sender, RoutedEventArgs e)
        {
            var newRegisterWindow = new Register();
            newRegisterWindow.ShowDialog(); //TODO test to see if this works .Show() is alternative
            //spawn registration window and close this window
        }

    }
}
